package com.baomidou.dynamic.controller;

import com.baomidou.dynamic.entity.User;
import com.baomidou.dynamic.mapper.UserMapper;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Validated
public class UserController {

  @Autowired
  private UserMapper userMapper;

  @PostMapping("/user/add")
  public String addUser(@NotEmpty @RequestParam String name, @Max(188) @RequestParam Integer age) {
    boolean result = userMapper.addUser(name, age);
    return result ? "新增用户成功" : "新增用户失败";
  }

  @PostMapping("/user/update")
  public String updateUser(@NotNull @RequestParam Integer id, @NotEmpty @RequestParam String name,
      @Max(188) @RequestParam Integer age) {
    boolean result = userMapper.updateUser(id, name, age);
    return result ? "更新用户成功" : "更新用户失败";
  }

  @GetMapping("/user/delete/{id}")
  public String deleteUser(@PathVariable("id") Integer id) {
    boolean result = userMapper.deleteUser(id);
    return result ? "删除用户成功" : "删除用户失败";
  }

  @GetMapping("/users")
  public List<User> users() {
    return userMapper.selectAll();
  }

}